<?php
include_once("../../commonFunctions.php");
$store = new Store($_SESSION['storeAbbrev']);
$dealer = new Dealer($_SESSION['dealerCode']);

$tt = new TT($_GET['ttid']);
if ($tt == 'selectOne') {
    return '';
}
$submittedBy = new Dealer($tt->submittedBy);
// If the current dealer reading this ticket was assigned to solve the ticket, update the status to read
if (strtolower($dealer->dealerCode) == strtolower($tt->assignedTo) && $tt->currentStatus == "Submitted") {
    echo '<center><font color="red">NOTE: This is your first time reading this ticket, which has been assigned to you.<br/>
                    It\'s status has just been changed to "Read"<br/>A text has just been sent to
                    ' . $submittedBy->firstName . ' ' . $submittedBy->lastName . ' notifying them of the change.' . '</font></center>';
    $tt->updateStatus('Read', $tt->id, $dealer->dealerCode, 'Set to Read through platform');
    $tt = new TT($_GET['ttid']);
    $result = tts_SendText($submittedBy->phone, 'TT Status Change', 'Your Ticket# ' . $tt->id . ' has just been read by ' . $dealer->fullName);
    if (!$result) {
        echo '<center>An attempt to send a text to the dealer who submitted this ticket failed.
            This is most likely because the wrong phone number is on file. A message has been sent to the
            system administrator.</center>';
        tts_EmailAdmin('VText Failed', 'A VText could not be sent to ' . $submittedBy->dealerCode . ' while trying to
                update the status of a ticket to read');
    }
}

if ($tt->tableName == 'computerissues') {
    echo "<table class=\"maintable\">";
    echo "<tr>
        <td class=\"tableheader\">TT ID</td>
        <td class=\"tableheader\">Submitted By</td>
        <td class=\"tableheader\">Assigned To</td>
        <td class=\"tableheader\">Type</td>
        <td class=\"tableheader\">Time Submitted</td>
        <td class=\"tableheader\">Time To Resolve</td>
        <td class=\"tableheader\">Description</td>
        <td class=\"tableheader\">PC</td>
        <td class=\"tableheader\">Status</td>
        </tr>";
    echo "<tr>";
    $assignedDealer = new Dealer($tt->assignedTo);
    echo "<td>" . $tt->id . "</td>";
    echo "<td>" . $tt->submittedByName . "</td>";
    echo "<td>" . $assignedDealer->fullName . "</td>";
    echo "<td>" . $tt->type . "</td>";
    echo "<td>" . $tt->dateTimeSubmitted . "</td>";
    echo "<td>" . $tt->dateTimeFinished . "</td>";
    echo "<td>" . $tt->description . "</td>";
    echo "<td>" . $tt->specialID . "</td>";
    echo "<td>" . $tt->currentStatus . "</td>";
    echo "</tr></table>";
} else if ($tt->tableName == 'maintenance' || $tt->tableName == 'merchandising') {
    echo "<table class=\"maintable\">";
    echo "<tr>
        <td class=\"tableheader\">TT ID</td>
        <td class=\"tableheader\">Submitted By</td>
        <td class=\"tableheader\">Assigned To</td>
        <td class=\"tableheader\">Type</td>
        <td class=\"tableheader\">Time Submitted</td>
        <td class=\"tableheader\">Time To Resolve</td>
        <td class=\"tableheader\">Description</td>
        <td class=\"tableheader\">Status</td>
        </tr>";
    $assignedDealer = new Dealer($tt->assignedTo);
    echo "<tr>";
    echo "<td>" . $tt->id . "</td>";
    echo "<td>" . $tt->submittedBy . "</td>";
    echo "<td>" . ucfirst(strtolower($assignedDealer->firstName)) . ' '
            . ucfirst(strtolower($assignedDealer->lastName)) . "</td>";
    echo "<td>" . $tt->type . "</td>";
    echo "<td>" . $tt->dateTimeSubmitted . "</td>";
    echo "<td>" . $tt->dateTimeFinished . "</td>";
    echo "<td>" . $tt->description . "</td>";
    echo "<td>" . $tt->currentStatus . "</td>";
    echo "</tr></table>";
}
?>
